package com.google.jia.utils;

/**
 * Created with IntelliJ IDEA.
 * User: Егор
 * Date: 01.07.12
 * Time: 1:45
 * To change this template use File | Settings | File Templates.
 */
public class DataConvertor {
    public static long bytesToDWORD(byte[] bytes)
    {
        if (bytes.length != 4)
            throw new ArrayIndexOutOfBoundsException("Incorrect size of array:\nIt should contain only 4 bytes");

        long result = 0;
        if (bytes[3] < 0) result += (-bytes[3] + 256) << 24;
        else result += bytes[3] << 24;

        if (bytes[2] < 0) result += (-bytes[2] + 256) << 16;
        else result += bytes[2] << 16;

        if (bytes[1] < 0) result += (-bytes[1] + 256) << 8;
        else result += bytes[1] << 8;

        if (bytes[0] < 0) result += (-bytes[0] + 256);
        else result += bytes[0];

        return result;
    }

    public static int bytesToWORD(byte[] bytes)
    {
        if (bytes.length != 4)
            throw new ArrayIndexOutOfBoundsException("Incorrect size of array:\nIt should contain only 2 bytes");
        int result = 0;

        if (bytes[1] < 0) result += (-bytes[1] + 256) << 8;
        else result += bytes[1] << 8;

        if (bytes[0] < 0) result += (-bytes[0] + 256);
        else result += bytes[0];

        return result;
    }
}
